﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNet.Random.Time
{
    /// <summary>
    /// 时间随机类，实现了接口IDateTimeRandServie
    /// 此类有以下属性
    /// Hour：小时
    /// Minute：分钟
    /// Second：秒
    /// Millisecond：毫秒
    /// </summary>
    public class TimeRandService : DotNet.Random.Interface.IDateTimeRandService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TimeRandService" /> class.
        /// </summary>
        /// <param name="hour">The hour.小时随机类</param>
        /// <param name="minute">The minute.分钟随机类</param>
        /// <param name="second">The second.秒随机类</param>
        /// <param name="mill">The mill.毫秒随机类</param>
        /// <exception cref="System.ArgumentNullException">除毫秒随机类可为null，其他随机类不能为null</exception>
        public TimeRandService(HourRandService hour, MinuteRandService minute,
            SecondRandService second, MillisecondRandService mill = null)
        {
            if (hour == null)
            {
                throw new ArgumentNullException("hour is null");
            }
            if (minute == null)
            {
                throw new ArgumentNullException("minute is null");
            }
            if (second == null)
            {
                throw new ArgumentNullException("second is null");
            }
            this.HourRand = hour;
            this.MinuteRand = minute;
            this.SecondRand = second;
            this.MillisecondRand = mill;
        }

        /// <summary>
        /// Gets the hour rand.
        /// 小时随机类
        /// </summary>
        /// <value>
        /// The hour rand.
        /// </value>
        protected HourRandService HourRand { get; private set; }

        /// <summary>
        /// Gets the minute rand.
        /// 分钟随机类
        /// </summary>
        /// <value>
        /// The minute rand.
        /// </value>
        protected MinuteRandService MinuteRand { get; private set; }

        /// <summary>
        /// Gets the second rand.
        /// 秒随机类
        /// </summary>
        /// <value>
        /// The second rand.
        /// </value>
        protected SecondRandService SecondRand { get; private set; }

        /// <summary>
        /// Gets the millisecond rand.
        /// 毫秒随机类
        /// </summary>
        /// <value>
        /// The millisecond rand.
        /// </value>
        protected MillisecondRandService MillisecondRand { get; private set; }

        /// <summary>
        /// Gets the hour.
        /// 小时
        /// </summary>
        /// <value>
        /// The hour.
        /// </value>
        public int Hour { get; private set; }

        /// <summary>
        /// Gets the minute.
        /// 分钟
        /// </summary>
        /// <value>
        /// The minute.
        /// </value>
        public int Minute { get; private set; }

        /// <summary>
        /// Gets the second.
        /// 秒
        /// </summary>
        /// <value>
        /// The second.
        /// </value>
        public int Second { get; private set; }

        /// <summary>
        /// Gets the millisecond.
        /// 毫秒
        /// </summary>
        /// <value>
        /// The millisecond.
        /// </value>
        public int Millisecond { get; private set; }

        /// <summary>
        /// Rands this instance.
        /// 随机生成时间方法
        /// 映射到虚方法中，扩展时，可重写
        /// </summary>
        /// <returns>
        /// 返回时间类型
        /// </returns>
        public virtual System.DateTime Rand()
        {
            this.Hour = this.HourRand.Rand();
            this.Minute = this.MinuteRand.Rand();
            this.Second = this.SecondRand.Rand();
            if (this.MillisecondRand != null)
            {
                this.Millisecond = this.MillisecondRand.Rand();
            }
            return new System.DateTime(1753, 1, 1, this.Hour, this.Minute, this.Second, this.Millisecond);
        }
    }
}
